//
//  SystemmsgTableViewController.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/12.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "SystemmsgTableViewController.h"
#import "SystemMessageCell.h"
#import "systemMessageDetailViewController.h"

#define URL_GetAnnouncements @"/api/announcements"

@interface SystemmsgTableViewController ()
@property(nonatomic,strong)NSArray *messageArr;
@end

@implementation SystemmsgTableViewController

- (void)viewDidLoad {
    self.title = @"系统消息";
    UIImage *leftImage=[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftButtonItem=[[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftButtonItem;
    
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"SystemMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"systemCell"];
    
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:URL_GetAnnouncements] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.messageArr=[BaseModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
        [self.tableView reloadData];
    }];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.messageArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"systemCell"];
    
    BaseModel *model=self.messageArr[indexPath.section];
    cell.titleLabel.text=model.title;
    cell.contentLabel.text=model.content;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BaseModel *model=self.messageArr[section];
    UIView *vi=[[UIView alloc]init];
    UILabel *la=[[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-160)/2,24,160,18)];
    la.textColor=[UIColor whiteColor];
    la.layer.masksToBounds=YES;
    la.layer.cornerRadius=8;
    la.font=[UIFont systemFontOfSize:15];
    la.textAlignment=1;
    la.backgroundColor=[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
    la.text=model.createdDate;
    [vi addSubview:la];
    return vi;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    systemMessageDetailViewController *detail=[systemMessageDetailViewController new];
    //UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
    BaseModel *model=self.messageArr[indexPath.section];
    detail.title = model.title;
    [detail getWebID:model.ID];
    [self.navigationController pushViewController:detail animated:YES];
}

@end
